<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="账号名称：" prop="account">
          <el-col :span="12">
            <el-input v-model="form.account" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <template v-if="type === 'add'">
          <el-form-item label="密码：" prop="accountPassword">
            <el-col :span="12">
              <el-input v-model="form.accountPassword" placeholder="请输入" show-password clearable />
            </el-col>
          </el-form-item>
          <el-form-item label="确认密码：" prop="confirmPassword">
            <el-col :span="12">
              <el-input v-model="form.confirmPassword" placeholder="请输入" show-password clearable />
            </el-col>
          </el-form-item>
        </template>
        <el-form-item label="角色：" prop="roleIdList">
          <el-col :span="12">
            <el-select v-model="form.roleIdList" placeholder="请选择" multiple clearable>
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="姓名：" prop="userName">
          <el-col :span="12">
            <el-input v-model="form.userName" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="手机号：" prop="mobile">
          <el-col :span="12">
            <el-input v-model="form.mobile" v-int placeholder="请输入" maxlength="11" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="性别：" prop="sex">
          <el-col :span="12">
            <el-select v-model="form.sex" placeholder="请选择" clearable>
              <el-option
                v-for="item in sexOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-col :span="12">
            <el-select v-model="form.status" placeholder="请选择" clearable>
              <el-option
                v-for="item in accountOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, editUser, getUserDetail, getAllRoleList } from '@/api/system'
import { ACCOUNT_STATUS, USER_SEX } from '@/utils/enum'
import { mobileReg } from '@/utils/const'
import { mapGetters } from 'vuex'
export default {
  name: 'AddUser',
  props: {
    type: {
      type: String,
      default: 'add' // add:新增 edit:编辑  password:修改密码
    },
    id: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    // var validatePass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入密码'))
    //   } else {
    //     if (this.ruleForm.checkPass !== '') {
    //       this.$refs.ruleForm.validateField('checkPass')
    //     }
    //     callback()
    //   }
    // }
    var validatePass2 = (rule, value, callback) => {
      if (value && value !== this.form.accountPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      loading: false,
      sexOptions: USER_SEX,
      accountOptions: ACCOUNT_STATUS,
      roleOptions: [],
      form: {
        account: '',
        accountPassword: '',
        confirmPassword: '',
        roleIdList: [],
        userName: '',
        mobile: '',
        sex: '',
        status: ''
      },
      rules: {
        account: [{ required: true, message: '请输入账号名称', trigger: ['blur'] }],
        accountPassword: [
          { required: true, message: '请输入密码', trigger: ['blur'] }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: ['blur'] },
          { validator: validatePass2, trigger: 'blur' }
        ],
        roleIdList: [{ required: true, message: '请选择角色', trigger: ['change', 'blur'] }],
        userName: [{ required: true, message: '请输入姓名', trigger: ['blur'] }],
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur'] },
          { pattern: mobileReg, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        sex: [{ required: true, message: '请选择性别', trigger: ['change', 'blur'] }],
        status: [{ required: true, message: '请选择状态', trigger: ['change', 'blur'] }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'currentEnterprise',
      'currentSite'
    ]),
    title() {
      if (this.type === 'add') {
        return '新增账号'
      } else if (this.type === 'edit') {
        return '编辑账号'
      } else {
        return '修改密码'
      }
    }
  },

  created() {
    if (this.type === 'edit' || this.type === 'add') {
      this.getAllRoleList()
      if (this.type === 'edit') {
        this.getDetail()
      }
    }
  },

  methods: {
    // 获取所有角色
    getAllRoleList() {
      const data = {
        enterpriseId: this.currentEnterprise.id
      }
      getAllRoleList(data).then(res => {
        this.roleOptions = res || []
      }).catch(() => {})
    },

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getUserDetail(data).then(res => {
        const { account, roleIdList, userName, mobile, sex, status } = res
        this.form = {
          account,
          roleIdList,
          userName,
          mobile,
          sex,
          status
        }
      }).catch(() => {
      })
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确认
    confirm() {
      if (this.type === 'add') {
        this.addHandle()
      } else if (this.type === 'edit') {
        this.editHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          addUser(data).then(() => {
            this.$message.success('新增成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.id
          }
          editUser(data).then(() => {
            this.$message.success('编辑成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
